<template>
  <div :style="{ width: showCalcType ? '340px' : '250px' }">
    <div class="title-box">
      <span class="subtitle ellipsis2" :title="cfg.chkitemName">
        <!--{{ cfg.chkitemName }}-->
        结构安全: 不得出现结构安全事故
        <span class="tip">{{ isSub ? '减分带负号(-)' : '' }}</span>
      </span>
      <el-checkbox :value="isThrowTerm" v-if="cfg.showThrowTerm" @click.native="changeScoreResult">甩项</el-checkbox>
    </div>
    <el-form class="form" ref="form" size="mini" :model="form" label-width="70px" :class="{ disabled: isThrowTerm }">
     <!-- <el-form-item label="事业部" prop="bussScoreNum" key="buss" :rules="itemRule" v-if="showObj.buss">
        <div class="item">
          <div class="el-input-group el-input-group&#45;&#45;append">
            <el-input-number v-model="form.bussScoreNum" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.bussCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.bussCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.bussRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>
      <el-form-item label="城市公司" prop="cityScoreNum" key="city" :rules="itemRule" v-if="showObj.city">
        <div class="item">
          <div class="el-input-group el-input-group&#45;&#45;append">
            <el-input-number v-model="form.cityScoreNum" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.cityCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.cityCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.cityRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>
      <el-form-item label="项目" prop="scoreInput" key="pro" :rules="itemRule" v-if="showObj.pro">
        <div class="item">
          <div class="el-input-group el-input-group&#45;&#45;append">
            <el-input-number v-model="form.scoreInput" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.proCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.proCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.proRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>-->
      <div class="belong-dept">经营单位：</div>
      <el-form-item label="集群" prop="bussScoreNum" key="buss" :rules="itemRule" v-if="showObj.buss">
        <div class="item">
          <div class="el-input-group el-input-group--append">
            <el-input-number v-model="form.bussScoreNum" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.bussCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.bussCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.bussRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>
       <el-form-item label="事业部" prop="bussScoreNum" key="buss" :rules="itemRule" v-if="showObj.buss">
        <div class="item">
          <div class="el-input-group el-input-group--append">
            <el-input-number v-model="form.bussScoreNum" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.bussCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.bussCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.bussRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>
      <el-form-item label="城市公司" prop="cityScoreNum" key="city" :rules="itemRule" v-if="showObj.city">
        <div class="item">
          <div class="el-input-group el-input-group--append">
            <el-input-number v-model="form.cityScoreNum" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.cityCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.cityCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.cityRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>
      <el-form-item label="项目" prop="scoreInput" key="pro" :rules="itemRule" v-if="showObj.pro">
        <div class="item">
          <div class="el-input-group el-input-group--append">
            <el-input-number v-model="form.scoreInput" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.proCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.proCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.proRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>
      <div class="belong-dept">职能部门：</div>
      <el-form-item label="集群" prop="bussScoreNum" key="buss" :rules="itemRule" v-if="showObj.buss">
        <div class="item">
          <div class="el-input-group el-input-group--append">
            <el-input-number v-model="form.bussScoreNum" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.bussCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.bussCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.bussRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>
      <el-form-item label="事业部" prop="bussScoreNum" key="buss" :rules="itemRule" v-if="showObj.buss">
        <div class="item">
          <div class="el-input-group el-input-group--append">
            <el-input-number v-model="form.bussScoreNum" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.bussCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.bussCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.bussRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>
      <div class="belong-dept">分管领导：</div>
      <el-form-item label="集群" prop="bussScoreNum" key="buss" :rules="itemRule" v-if="showObj.buss">
        <div class="item">
          <div class="el-input-group el-input-group--append">
            <el-input-number v-model="form.bussScoreNum" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.bussCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.bussCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.bussRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>
      <el-form-item label="事业部" prop="bussScoreNum" key="buss" :rules="itemRule" v-if="showObj.buss">
        <div class="item">
          <div class="el-input-group el-input-group--append">
            <el-input-number v-model="form.bussScoreNum" :min="minVal" :max="maxVal" :precision="2" :controls="false" :disabled="isThrowTerm || !!form.bussCalcType" style="width: 100%" />
            <div class="el-input-group__append append-unit">分</div>
          </div>
          <el-select class="select" v-model="form.bussCalcType" clearable :disabled="isThrowTerm" @change="changeToZero" v-if="showCalcType">
            <el-option v-for="dict in dict.type.cq_eval_quality_calc" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </div>
        <el-input v-model="form.bussRemark" type="textarea" placeholder="描述" :rows="1" autosize :maxlength="255" :disabled="isThrowTerm" style="margin-top: 5px" />
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// 加减分对象。addsubObj 有值时
import { CQ_EVAL_SCORE_TYPE, CQ_EVAL_ADDSUB_OBJ, CQ_EVAL_RESULT_ENUM as RESULT } from '@/utils/baseData'
import FormMixin from './mixin'
export default {
  mixins: [FormMixin],
  dicts: ['cq_eval_quality_calc'],
  data() {
    return {
      formInitVals: {
        chkitemScoreId: '',
        versionId: '',
        versionCode: '',
        chkitemCfgId: '',
        chkitemCode: '',
        scoreType: CQ_EVAL_SCORE_TYPE.ADD,
        bussScoreNum: 0,
        cityScoreNum: 0,
        scoreInput: 0,
        bussCalcType: '',
        cityCalcType: '',
        proCalcType: '',
        bussRemark: '',
        cityRemark: '',
        proRemark: '',
        scoreLevel: '',
        scoreResult: '',
        isValid: 0,
        isExce: 0
      }
    }
  },
  computed: {
    isThrowTerm() {
      return this.form.scoreResult === RESULT.THROWTERM
    },
    isSub() {
      // 是否为减分
      const scoreType = this.cfg?.scoreType
      return scoreType === CQ_EVAL_SCORE_TYPE.SUB
    },
    minVal() {
      return this.isSub ? -100 : 0
    },
    maxVal() {
      return this.isSub ? 0 : 100
    },
    itemRule() {
      return this.isThrowTerm ? [] : this.isSub ? [{ required: true, message: '取值范围：-100~0，减分带负号（-）' }] : [{ required: true, message: '请输入分数' }]
    },
    showObj() {
      const str = this.cfg?.addsubObj || ''
      const data = str.split(',').filter(i => i)
      return {
        buss: data.includes(CQ_EVAL_ADDSUB_OBJ.BUSSNIESS),
        city: data.includes(CQ_EVAL_ADDSUB_OBJ.CITY),
        pro: data.includes(CQ_EVAL_ADDSUB_OBJ.PROJECT)
      }
    },
    showCalcType() {
      return !!this.cfg?.qualityRuleType
    }
  },
  methods: {
    changeScoreResult(e) {
      // 防止点击事件冒泡两次
      if (e.target.tagName === 'INPUT') return

      const { form } = this
      if (this.isThrowTerm) return (form.scoreResult = '')
      form.scoreResult = RESULT.THROWTERM
      form.bussScoreNum = 0
      form.cityScoreNum = 0
      form.scoreInput = 0
      form.bussCalcType = ''
      form.cityCalcType = ''
      form.proCalcType = ''
      form.bussRemark = ''
      form.cityRemark = ''
      form.proRemark = ''
      this.$refs.form.clearValidate()
    },
    changeToZero() {
      const showObj = this.showObj
      const { bussCalcType, cityCalcType, proCalcType } = this.form
      if (!showObj.buss || bussCalcType) this.form.bussScoreNum = 0
      if (!showObj.city || cityCalcType) this.form.cityScoreNum = 0
      if (!showObj.pro || proCalcType) this.form.scoreInput = 0
    }
  }
}
</script>
<style lang="scss" scoped>
.title-box {
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #ebeef5;
  padding: 10px 10px 5px;
  .subtitle {
    padding-right: 10px;
  }
  .tip {
    font-size: 12px;
    color: #999;
  }
}
.form {
  padding: 10px;
  &.disabled {
    opacity: 0.8;
    pointer-events: none;
  }
}
.item {
  display: flex;
  align-items: center;
}
.select {
  width: 100%;
  margin-left: 5px;
}
.append-unit {
  padding: 0 10px;
}
  .belong-dept {
    margin: 0 0 10px 3px;
    font-weight: 700;
  }
</style>
